package cn.dt70.service.impl;

import cn.dt70.dao.aqbUser.AqbUserMapper;
import cn.dt70.pojo.AqbUser;
import cn.dt70.service.AqbUserService;
import cn.dt70.utils.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("aqbUserService")
public class AqbUserServiceImpl implements AqbUserService{

    private static final Logger LOGGER = Logger.getLogger(AqbUserServiceImpl.class);

    @Resource
    private AqbUserMapper aqbUserMapper;


    @Override
    public AqbUser addUser(AqbUser aqbUser) {
        int row = 0;
        try {
           row=aqbUserMapper.saveUser(aqbUser);
            if (0==row) {
                LOGGER.debug("aqbUserMapper.saveUser 添加失败");
            } else {
                LOGGER.debug("aqbUserMapper.saveUser 添加成功");
            }

        } catch (Exception e) {
            LOGGER.error("aqbUserMapper.saveUser 添加异常" + e.getMessage());
        }
        return aqbUser;
    }

     @Override
     public AqbUser doLogin(String userCode, String password) {
         AqbUser aqbUser=null;
         try {
            aqbUser = aqbUserMapper.qryByuserCode(userCode);
             if (null == aqbUser) {
                 LOGGER.debug("aqbUserMapper.qryByuserCode 查询用户为空!");
             } else {
                 if (!password.equals(aqbUser.getPassword())) {
                     LOGGER.debug("aqbUserMapper.qryByuserCode 输入的账号和密码不匹配!");
                     aqbUser=new AqbUser();
                 } else {
                     LOGGER.debug("aqbUserMapper.qryByuserCode 登陆成功>>>>>!");
                 }
             }
         } catch (Exception e) {
             LOGGER.error("aqbUserMapper.qryByuserCode 异常:" + e.getMessage());
             aqbUser=new AqbUser();
         }
         return aqbUser;
     }

    @Override
    public AqbUser getByUserCode(String userCode) {
       AqbUser aqbUser=null;
        try {
            aqbUser=aqbUserMapper.qryByuserCode(userCode);
            if(null==aqbUser){
                LOGGER.debug("aqbUserMapper.qryByuserCode 查询失败");
                aqbUser=new AqbUser();
            }else{
                LOGGER.debug("aqbUserMapper.qryByuserCode 查询成功");
            }
        } catch (Exception e) {
            LOGGER.debug("aqbUserMapper.qryByuserCode 查询异常"+e.getMessage());
            aqbUser=new AqbUser();
        }
        return aqbUser;
    }

    @Override
    public AqbUser getByUserId(Integer userId) {
        AqbUser aqbUser=null;
        try {
            aqbUser=aqbUserMapper.qryByUserId(userId);
            if(aqbUser==null){
                LOGGER.debug("aqbUserMapper.qryByUserId 查询失败");
                aqbUser=new AqbUser();
            }else{
                LOGGER.debug("aqbUserMapper.qryByUserId 查询成功");
            }
        } catch (Exception e) {
            LOGGER.debug("aqbUserMapper.qryByUserId 查询异常");
            aqbUser=new AqbUser();
        }
        return aqbUser;
    }

    @Override
    public boolean modifyAqbUser(AqbUser aqbUser) {
        boolean flag=false;
        int row=0;
        try {
            row=aqbUserMapper.updateAqbUser(aqbUser);
            if(row != 1){
              LOGGER.debug("aqbUserMapper.updateAqbUser 修改失败");
            }else{
              LOGGER.debug("aqbUserMapper.updateAqbUser 修改成功");
                flag=true;
            }
        } catch (Exception e) {
            LOGGER.error("aqbUserMapper.updateAqbUser 修改异常"+e.getMessage());
        }
        return flag;
    }

//    后台分页查询用户信息
    @Override
    public List<AqbUser> getAllUserInfo(String userCode, String userName, PageSupport ph) {
        List<AqbUser> userinfoList = new ArrayList<>();
//        if("".equals(userCode)){
//            userCode = null;
//        }
        try {
            ph.setTotalCount(aqbUserMapper.qryCount(userCode,userName));
            userinfoList = aqbUserMapper.getAllUserInfo(userCode,userName,(ph.getCurrentPageNo()-1)*ph.getPageSize(),ph.getPageSize());
//           userinfoList = userMapper.getAllUserInfo(userCode,userName,ph);
            if (userinfoList == null){
                LOGGER.debug("AqbUserServiceImpl.getAllUserInfo 分页查询为空");
            }else {
                LOGGER.debug("AqbUserServiceImpl.getAllUserInfo 分页查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("AqbUserServiceImpl.getAllUserInfo 异常"+e.getMessage());
        }
        return userinfoList;
    }

//    根据用户编码获取用户详细信息
    @Override
    public AqbUser getUserInfoById(String userCode){
        AqbUser aqbUser = new AqbUser();
        try {
            aqbUser = aqbUserMapper.getUserInfoById(userCode);
        } catch (Exception e) {
            LOGGER.error("AqbUserServiceImpl.getAllUserInfoById 异常"+e.getMessage());
        }
        return aqbUser;
    }

}
